#include "playscene.h"

Playscene::Playscene(QWidget *parent) : QWidget(parent)
{
       initSence();
}

//初始化场景
void Playscene::initSence()
{


    //设置窗口固定尺寸
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //设置标题
    setWindowTitle(GAME_TITLE);
    //设置标题图片
    setWindowIcon(QIcon(":/rec.image/Icon.png"));


    //定时器设置
    m_timer.setInterval(GAME_RATE);

    //开始游戏
    playGame();

    //返回按钮
    back_btn=new QPushButton(this);//设置所属场景
   back_btn->setFixedSize(50,50);//设置固定大小
    back_btn->move(GAME_WIDTH-50,0);//设置位置
    back_btn->setIcon(QIcon(":/rec.image/7d3fdb1f21fc44e0897c5ce2b5990410.png"));
    back_btn->setIconSize(QSize(50,50));


    //点击返回按钮
    connect(back_btn,&QPushButton::clicked,[=](){
        //发送返回信号
        emit(returnmain());

    });

    //技能按钮
    big_btn=new QPushButton(this);
    big_btn->setFixedSize(80,80);
    big_btn->move(GAME_WIDTH-80,550);
    big_btn->setIcon(QIcon(":/rec.image/Big.png"));
    big_btn->setIconSize(QSize(80,80));

    //点击技能按钮
    connect(big_btn,&QPushButton::clicked,[=](){
        emit(release_big());
    });

    connect(this,&Playscene::release_big,[=](){
        if(is_big==0)
        {
            if(m_hero.m_HERO_Blue>0)
            {
                is_big=1;
                //持续五秒
                QTimer::singleShot(5000,[=](){
                    is_big=0;
                });
            }
        }
    });
}


//启动游戏
void Playscene::playGame()
{
   //m_timer.start();

   //监听定时器发出的信号
   connect(&m_timer,&QTimer::timeout,[=](){
       //更新地图元素
       updatePosition();
       //绘制
       update();
   });
}
//更新所有元素的坐标
void Playscene::updatePosition()
{

  //地图滚动
  m_map.mapPosition();

  //发射子弹
  m_hero.shoot();

  //是否放大招
  if(is_big)
  {
      m_hero.shoot2();
      m_hero.shoot3();
      m_hero.shoot4();
  }



  //计算所有非空闲子弹当前坐标
  for(int i=0;i<BULL_NUM;i++)
  {
      //如果非空闲，计算发射位置
      if(m_hero.AddNum==1)
      {
          if(m_hero.m_bullets1[i].m_Free==0)
          {
              m_hero.m_bullets1[i].updatePosition();

          }
      }
      else if(m_hero.AddNum==2)
      {
          if(m_hero.m_bullets1[i].m_Free==0)
          {
              m_hero.m_bullets1[i].updatePosition();
          }

          if(m_hero.m_bullets3[i].m_Free==0)
          {
              m_hero.m_bullets3[i].updatePosition();
          }
      }
      else if(m_hero.AddNum==3)
      {
          if(m_hero.m_bullets1[i].m_Free==0)
          {
              m_hero.m_bullets1[i].updatePosition();
          }


          if(m_hero.m_bullets2[i].m_Free==0)
          {
              m_hero.m_bullets2[i].updatePosition();
          }

          if(m_hero.m_bullets3[i].m_Free==0)
          {
              m_hero.m_bullets3[i].updatePosition();
          }
      }
  }
  //特殊子弹

  //特殊子弹的发射
  for(int i=0;i<BULL_NUM2;i++)
  {

      if(m_hero.m_special_bullets[i].m_Free==0)
      {
          m_hero.m_special_bullets[i].updatePosition2();
      }

  }

  //特殊子弹二
  for(int i=0;i<BULL_NUM3;i++)
  {

      if(m_hero.m_special_bullets2[i].m_Free==0)
      {
          m_hero.m_special_bullets2[i].updatePosition3();
      }

  }
  //特殊子弹三
  for(int i=0;i<BULL_NUM3;i++)
  {
      if(m_hero.m_special_bullets3[i].m_Free==0)
      {
          m_hero.m_special_bullets3[i].updatePosition4();
      }
  }

}

//绘制到屏幕中
void Playscene::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    //绘制飞机
    painter.drawPixmap(m_hero.m_x,m_hero.m_y,m_hero.m_Plane);

    //绘制子弹
    for(int i=0;i<BULL_NUM;i++)
    {
        //如果非空闲，开始绘制
        if(m_hero.AddNum==1)
        {
            if(m_hero.m_bullets1[i].m_Free==0)
            {
               painter.drawPixmap(m_hero.m_bullets1[i].m_x,m_hero.m_bullets1[i].m_y,m_hero.m_bullets1[i].m_Bullet);
            }
        }
        else if(m_hero.AddNum==2)
        {
            if(m_hero.m_bullets1[i].m_Free==0)
            {
                painter.drawPixmap(m_hero.m_bullets1[i].m_x,m_hero.m_bullets1[i].m_y,m_hero.m_bullets1[i].m_Bullet);
            }

            if(m_hero.m_bullets3[i].m_Free==0)
            {
                painter.drawPixmap(m_hero.m_bullets3[i].m_x,m_hero.m_bullets3[i].m_y,m_hero.m_bullets3[i].m_Bullet);
            }
        }
        else if(m_hero.AddNum==3)
        {
            if(m_hero.m_bullets1[i].m_Free==0)
            {
                painter.drawPixmap(m_hero.m_bullets1[i].m_x,m_hero.m_bullets1[i].m_y,m_hero.m_bullets1[i].m_Bullet);
            }

            if(m_hero.m_bullets2[i].m_Free==0)
            {
                painter.drawPixmap(m_hero.m_bullets2[i].m_x,m_hero.m_bullets2[i].m_y,m_hero.m_bullets2[i].m_Bullet);
            }

            if(m_hero.m_bullets3[i].m_Free==0)
            {
                painter.drawPixmap(m_hero.m_bullets3[i].m_x,m_hero.m_bullets3[i].m_y,m_hero.m_bullets3[i].m_Bullet);
            }
        }

    }
    //绘制特殊子弹

    for(int i=0;i<BULL_NUM2;i++)
    {
            if(m_hero.m_special_bullets [i].m_Free==0)
            {
                painter.drawPixmap(m_hero.m_special_bullets[i].m_x,m_hero.m_special_bullets[i].m_y,m_hero.m_special_bullets[i].m_Bullet);
            }

    }
    //绘制第二种特殊子弹
    for(int i=0;i<BULL_NUM3;i++)
    {
            if(m_hero.m_special_bullets2[i].m_Free==0)
            {
                painter.drawPixmap(m_hero.m_special_bullets2[i].m_x,m_hero.m_special_bullets2[i].m_y,m_hero.m_special_bullets2[i].m_Bullet);
            }
    }
    //绘制第三种特殊子弹
    for(int i=0;i<BULL_NUM3;i++)
    {
        if(m_hero.m_special_bullets3[i].m_Free==0)
        {
            painter.drawPixmap(m_hero.m_special_bullets3[i].m_x,m_hero.m_special_bullets3[i].m_y,m_hero.m_special_bullets3[i].m_Bullet);
        }
    }

}



//鼠标事件
void Playscene::mouseMoveEvent(QMouseEvent *event)
{
    int x=event->x()-m_hero.m_Plane.width()*0.5;
     int y=event->y()-m_hero.m_Plane.height()*0.5;

     //边界检测
     if(x<=0)
     {
         x=0;
     }
     if(x>=GAME_WIDTH-m_hero.m_Rect.width())
     {
         x=GAME_WIDTH-m_hero.m_Rect.width();
     }
     if(y<=0)
     {
         y=0;
     }
     if(y>=GAME_HEIGHT-m_hero.m_Rect.height())
     {
         y=GAME_HEIGHT-m_hero.m_Rect.height();
     }
     m_hero.setPOsition(x,y);
//     m_Wing_left.setPOsition(x-60,y+20);
//     m_Wing_right.setPOsition(x+130,y+20);
}


